import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import './plugins/element.js'
import store from './store'
import router from "./router/index";
import less from 'less';

Vue.config.productionTip = false
Vue.use(less)

router.beforeEach((to, from, next) => {
  // 判断用户是否登录
  if (to.meta.requireAuth && !isLoggedIn()) {
    // 如果需要登录且用户未登录，则重定向到登录页面
    next('/login')
  } else {
    // 用户已登录或不需要登录，继续导航
    next()
  }

  // 访问login页面时，若已登陆，则直接跳转首页
  if(to.path == "/login"){
    if (isLoggedIn()){
      next('/')
    }
  }
})

new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')


function isLoggedIn() {
  // 返回true表示用户已登录，返回false表示用户未登录
  if(localStorage.getItem('loginStatus') && localStorage.getItem('userToken')){
    store.state.loginStatus = true;
    store.state.userToken = localStorage.getItem('userToken');
  }
  return store.state.loginStatus;
}